package cn.virens.library.http.response;

import org.json.JSONException;
import org.xutils.http.app.ResponseParser;
import org.xutils.http.request.UriRequest;

import java.lang.reflect.Type;

/**
 * 后台JSON对象(ServerResponse) - 响应解析转换类
 * Created by loioi on 2016/11/22.
 */
public class ServerResponseParser implements ResponseParser {

    @Override
    public void checkResponse(UriRequest request) throws Throwable {

    }

    @Override
    public Object parse(Type resultType, Class<?> resultClass, String result) throws Throwable {
        try {
            ServerResponse response = new ServerResponse(result);
            if (response.success()) {
                return response;
            } else {
                throw new ResponseException(response);
            }
        } catch (JSONException e) {
            throw new ResponseException("解析失败", e);
        }
    }
}
